layui.use(['table', 'admin', 'ax', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    /**
     * Product Table Management 商品表管理
     */
    var BasicGoods = {
        tableId: "basicGoodsTable"
    };

    /**
     * Initialize Table Columns 初始化表格的列
     */
    BasicGoods.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'Primary Key'},
            {field: 'goodsCustomerName', title: 'Customer',width:130, style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'goodsName', sort: true, title: 'Product Name',width:200, style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: 'Product Code',width:130, style:'font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: 'Item No.',width:130, style:'font-weight: 600;'},
            {field: 'goodsSupportvolume', title: 'Per Pallet Qty', style:'font-weight: 600;'},
            {field: 'goodsVolume', title: 'Volume (M3)'},
            {field: 'goodsWeight', title: 'Weight'},
            {field: 'goodsCharging', title: 'Pricing'},
            {field: 'goodsBigtype', title: 'Product Category'},
            {field: 'goodsType', title: 'Type'},
            {field: 'goodsUnit', title: 'Unit'},
            {field: 'goodsPacking', title: 'Packaging'},
            {field: 'goodsSpecs', title: 'Specifications'},
            {align: 'center', width:70, toolbar: '#tableBar', title: 'Action', fixed:'right'},
        ]];
    };

    /**
     * Click Search Button 点击查询按钮
     */
    BasicGoods.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(BasicGoods.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };

    // Render table 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicGoods.tableId,
        url: Feng.ctxPath + '/outOrderDetailed/ChooseGoods?goodsCustomerId='+Feng.getUrlParam("goodsCustomerId"),
        page: true,
        limit: 30,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicGoods.initColumn()
    });

    // Search button click event 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicGoods.search();
    });

    // Fill product data 回填商品数据
    function confirm(data){
        parent.OutOrderDetailedInfoDlg.data.detailedGoodsname = data.goodsName;//Product Name
        parent.OutOrderDetailedInfoDlg.data.detailedGoodsId = data.id;//Product ID
        parent.OutOrderDetailedInfoDlg.data.detailedGoodscode = data.goodsNumber;//Product Code
        parent.OutOrderDetailedInfoDlg.data.goodsNo = data.goodsNo;

        parent.OutOrderDetailedInfoDlg.data.detailedLargeclass = data.goodsBigtype;//Product Category
        parent.OutOrderDetailedInfoDlg.data.detailedGoodstype = data.goodsType;//Type
        parent.OutOrderDetailedInfoDlg.data.goodsUnit = data.goodsUnit;
        parent.OutOrderDetailedInfoDlg.data.detailedPacking = data.goodsPacking;//Packaging
        parent.OutOrderDetailedInfoDlg.data.goodsSpecs = data.goodsSpecs;

        parent.OutOrderDetailedInfoDlg.data.detailedWeight = data.goodsWeight;//Weight
        parent.OutOrderDetailedInfoDlg.data.detailedVolume = data.goodsVolume;//Volume
        parent.OutOrderDetailedInfoDlg.data.detailedPrice = data.goodsCharging;//Pricing
        var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);

    }

    // Double click row event 双击行事件
    table.on('rowDouble(' + BasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
        confirm(data);
    });

    // Toolbar click event 工具条点击事件
    table.on('tool(' + BasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'confirm') {
            confirm(data);
        }
    });
});
